package com.yiyuan.app.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.yiyuan.app.entity.po.MRankPushCoinsPO;
import com.yiyuan.app.mapper.MRankPushCoinsExtMapper;
import com.yiyuan.app.mapper.MRankPushCoinsMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
@AllArgsConstructor
public class MRankPushCoinsDAO extends BaseDAO<MRankPushCoinsMapper, MRankPushCoinsPO> {

    private final MRankPushCoinsExtMapper mRankPushCoinsExtMapper;

    public List<MRankPushCoinsPO> findWeekRankList(LocalDate startOfWeek, LocalDate endOfWeek, Integer size) {
        LambdaQueryWrapper<MRankPushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRankPushCoinsPO::getStartDate, startOfWeek);
        queryWrapper.eq(MRankPushCoinsPO::getEndDate, endOfWeek);
        queryWrapper.orderByDesc(MRankPushCoinsPO::getPushCoinsValue);
        queryWrapper.last("limit 0," + size);
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<MRankPushCoinsPO> findYesWeekRankList(LocalDate startOfLastWeek, LocalDate endOfLastWeek) {
        LambdaQueryWrapper<MRankPushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRankPushCoinsPO::getStartDate, startOfLastWeek);
        queryWrapper.eq(MRankPushCoinsPO::getEndDate, endOfLastWeek);
        queryWrapper.eq(MRankPushCoinsPO::getIsRank, Boolean.TRUE);
        queryWrapper.orderByAsc(MRankPushCoinsPO::getRankNo);
        return getBaseMapper().selectList(queryWrapper);
    }

    public MRankPushCoinsPO findUserRank(LocalDate startOfWeek, LocalDate endOfWeek, Long userId) {
        LambdaQueryWrapper<MRankPushCoinsPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRankPushCoinsPO::getStartDate, startOfWeek);
        queryWrapper.eq(MRankPushCoinsPO::getEndDate, endOfWeek);
        queryWrapper.eq(MRankPushCoinsPO::getUserId, userId);
        return getBaseMapper().selectOne(queryWrapper);
    }

    public int addPushCoinsValue(Long rankId, Integer pushCoinsValue) {
        return mRankPushCoinsExtMapper.addPushCoinsValue(rankId, pushCoinsValue);
    }
}
